package com.easthome.exception;

import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author cxiaoy
 * @Date 2022/10/13 14:06
 */
@ControllerAdvice
public class MyExceptionHandler {

//    @ExceptionHandler(NameBuHaoTingException.class)
//    public String nameException(NameBuHaoTingException e, Model model){
//        //这个方法，专门处理name不好听异常的。
//        System.out.println("统一处理异常信息："+e.getMessage());
//        model.addAttribute("message",e.getMessage());
//        return "jsp/error";
//    }
//
//    //这个方法，专门处理公司内部自定义异常的
//    @ExceptionHandler(BussinessException.class)
//    public String bussinessException(BussinessException e, Model model){
//        System.out.println("统一处理异常信息："+e.getMsg()+","+e.getCode());
//        model.addAttribute("message",e.getMsg());
//        model.addAttribute("code",e.getCode());
//        return "jsp/error";
//    }

    //这是普通请求，
//    @ExceptionHandler(Exception.class)
//    public String exception(Exception e, Model model){
//        if(e instanceof BussinessException){//BussinessException
//            //e.printStackTrace();
//            model.addAttribute("message",((BussinessException) e).getMsg());
//            model.addAttribute("code",((BussinessException) e).getCode());
//        }else{//java级的其他异常
//            model.addAttribute("message",e.getMessage());
//        }
//        return "jsp/error";
//    }

    @ExceptionHandler(Exception.class)
    public String exception(Exception e, Model model, HttpServletRequest request, HttpServletResponse response){
        e.printStackTrace();
        if ((request.getHeader("accept").indexOf("application/json") > -1 || (request
                .getHeader("X-Requested-With") != null && request.getHeader(
                "X-Requested-With").indexOf("XMLHttpRequest") > -1))) {//代表它是个ajax
            //处理ajax的事
            try {
                if(e instanceof BussinessException){//BussinessException
                    response.getWriter().write(((BussinessException) e).getMsg());
                }else{//java级的其他异常
                    response.getWriter().write(e.getMessage());
                }
            } catch (IOException ioException) {
                ioException.printStackTrace();
            }
            return null;
        }else{//普通 请求的事。
            if(e instanceof BussinessException){//BussinessException
                model.addAttribute("message",((BussinessException) e).getMsg());
                model.addAttribute("code",((BussinessException) e).getCode());
//                return "redirect:/static/jsp/error.jsp?message="+((BussinessException) e).getMsg();
            }else{//java级的其他异常
                model.addAttribute("message",e.getMessage());
//                return "redirect:/static/jsp/error.jsp?message="+e.getMessage();
            }
            return "redirect:/static/jsp/error.jsp";

        }

    }
}
